from model.BaseModel import *

class ProjectVersionModel(BaseModel):
    def __init__(self):
        BaseModel.__init__(self)

    def getDatas(self,id,fields,page,num,orderfield,asc):
        cnt = self.getCountNum(id)
        data = self.getData(id,fields,page,num,orderfield,asc)
        return {"code": 0, "msg": "获取数据成功", "count": cnt, "data": data}

    def getCountNum(self,id):
        sql = ("select count(1) from ("
               "select pm.version_no,p.project_name,gr.uri,s.server_name,s.ip "
               "from projects p "
               "left join projects_map pm on p.id = pm.project_id "
               "left join git_repos gr on pm.git_repos_id = gr.id "
               "left join servers s on pm.server_id = s.id "
               "where p." + str(id) + ""
               ") a")
        rs = self.db.executeSql(sql)
        cnt = rs[0][0]
        return cnt

    def getData(self,id,fields,page,num,orderfield,asc):
        if int(page) <= 1:
            start = 0
        else:
            start = (int(page) - 1) * int(num)
        sql = ("select * from ("
               "select pm.version_no,p.project_name,gr.uri,s.server_name,s.ip "
               "from projects p "
               "left join projects_map pm on p.id = pm.project_id "
               "left join git_repos gr on pm.git_repos_id = gr.id "
               "left join servers s on pm.server_id = s.id "
               "where p." + str(id) + ""
               ") a order by " + orderfield + " " + asc + " limit " + str(start) + "," + str(num) + ";")
        return self.getMapData(self.db.executeSql(sql),fields)

